﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Xml.Serialization;
using System.Data;
using System.IO;

namespace _1041326_1041339_1041433.AdminPages
{
    public partial class UserAdmin : System.Web.UI.Page
    {

        String url = "http://localhost:1871/Service.svc";
        private String[] arr_role = new String[2] { "Quản trị viên", "Nhân viên" };
        private String[] arr_status = new String[2] { "Không hoạt động", "Hoạt động" };
        //private List<ArrayOfNguoiDungDTONguoiDungDTO> users;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                User_List();
                lbThongBao.Text = "";
                Load_Control();
            }
        }

        protected void Load_Control()
        {
            btnThemMoi.Enabled = false;
            btnCapNhat.Enabled = false;
            btnLamLai.Enabled = false;
            btnHuy.Enabled = false;
        }

        protected void LoadControl()
        {
           // btnThemMoi.Enabled = true;
            btnCapNhat.Enabled = true;
            btnLamLai.Enabled = true;
            btnHuy.Enabled = true;
        }
        private void User_List()
        {
            WebRequest wr = WebRequest.Create(url + "/user/getAll");
            WebResponse wp = wr.GetResponse();
            XmlSerializer serial = new XmlSerializer(typeof(ArrayOfNguoiDungDTO));
            ArrayOfNguoiDungDTO ds = (ArrayOfNguoiDungDTO)serial.Deserialize(wp.GetResponseStream());

            gvUser.DataSource = ds.Items;
            gvUser.DataBind();

            #region note1
            //  users = new List<ArrayOfNguoiDungDTONguoiDungDTO>(ds.Items);

            //DataTable dt = new DataTable();
            //dt.Columns.Add("#");
            //dt.Columns.Add("ID");
            //dt.Columns.Add("Email");
            //dt.Columns.Add("Họ tên");
            //dt.Columns.Add("Phân quyền");
            //dt.Columns.Add("Tình trạng");
            //DataRow row = null;

            //for (int i = 0; i < ds.Items.Length; i++)
            //{
            //    row = dt.NewRow();
            //    row["#"] = (i + 1).ToString();
            //    row["ID"] = ds.Items[i].MaNguoiDung;
            //    row["Email"] = ds.Items[i].Email;
            //    // row["Email"] = "<a href='User_Update.aspx?email=" + ds.Items[i].Email + "'>" + ds.Items[i].Email + "</a>";
            //    row["Họ tên"] = ds.Items[i].Ten;
            //    row["Phân quyền"] = arr_role[int.Parse(ds.Items[i].Quyen)];
            //    row["Tình trạng"] = arr_status[int.Parse(ds.Items[i].TinhTrang)];
            //    dt.Rows.Add(row);
            //} 
            #endregion
        }

        #region note2
        //protected void gvUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
        //{            
        //    gvUser.PageIndex = e.NewPageIndex;
        //    gvUser.DataBind();
        //}

        //protected void gvUser_RowDeleting(object sender, GridViewDeleteEventArgs e)
        //{
        //    int selectedIndex = gvUser.SelectedIndex;
        //    string ma = gvUser.Rows[selectedIndex].Cells[0].Text;
        //    string tinhTrang = gvUser.Rows[gvUser.SelectedIndex].Cells[4].Text;
        //    //string tt =
        //    WebRequest wr = WebRequest.Create(url + "/delete/" + ma + "/" + tinhTrang);
        //    WebResponse wp = wr.GetResponse();
        //    XmlSerializer serial = new XmlSerializer(typeof(ArrayOfNguoiDungDTO));
        //    ArrayOfNguoiDungDTO ds = (ArrayOfNguoiDungDTO)serial.Deserialize(wp.GetResponseStream());
        //    User_List();
        //} 
        #endregion

        protected void gvUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvUser.PageIndex = e.NewPageIndex;
            gvUser.DataBind();
        }

        protected void gvUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedIndex = gvUser.SelectedIndex;
            //if (selectedIndex != -1)
            //{
                LoadControl();
                txtHoTen.Text = gvUser.Rows[selectedIndex].Cells[1].Text;
                txtEmail.Text = gvUser.Rows[selectedIndex].Cells[2].Text;
                int role = int.Parse(gvUser.Rows[selectedIndex].Cells[3].Text);
                int trash = int.Parse(gvUser.Rows[selectedIndex].Cells[4].Text);
                rbQuanTri.Checked = (role == 0);
                rbNhanVien.Checked = !rbQuanTri.Checked;
                rbHoatDong.Checked = (trash == 1);
                rbKhongHoatDong.Checked = !rbHoatDong.Checked;
            //}
            //else
            //{
            //    lbThongBao.Text = "Vui lòng chọn giá trị để thao tác!";
            //}
        }

        protected void gvUser_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                 int selectedIndex = gvUser.SelectedIndex;
                 if (selectedIndex != -1)
                 {


                     string ma = gvUser.DataKeys[e.RowIndex].Value.ToString();
                     string tinhTrang = gvUser.Rows[selectedIndex].Cells[4].Text;
                     WebRequest wr = WebRequest.Create(url + "/user/deleteUser/" + ma + "/" + tinhTrang);
                     wr.Method = "DELETE";
                     WebResponse wp = wr.GetResponse();
                     lbThongBao.Text = "Xóa thành công";
                     User_List();
                 }
                 else {
                     lbThongBao.Text = "Lỗi: Vui lòng chọn giá trị để thao tác!";
                 }


            }
            catch (Exception ex)
            {
                lbThongBao.Text = "Lỗi: " + ex.Message;
            }
        }

        protected void clean()
        {
            txtHoTen.Text = "";
            txtEmail.Text = "";
            rbQuanTri.Checked = true;
            rbHoatDong.Checked = true;
        }

        protected void btnLamLai_Click(object sender, EventArgs e)
        {
            clean();
        }


        protected void btnHuy_Click(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }


    }
}